home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / ANS199A.ZIP / record.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-25  |  6.1 KB  |  248 lines

  1. // (C) copyright 1996 Sacha Prins
  2.  
  3. #include <string.h>
  4. #include <stream.h>
  5. #include <stdlib.h>
  6. #include "\work\modemengine\modemengine.hpp"
  7.  
  8. int bits_per_sample=4;
  9. int device= 1;
  10. char * file_name= NULL;
  11. char * com_port= NULL;
  12. BOOL debug= FALSE;
  13.  
  14. void usage();
  15. void parse_arg(int argc, char * argv[]);
  16. void record();
  17. MODEM_ENGINE::MODEMRESPONSE response (MODEM_ENGINE & me, int timeout=600);
  18.  
  19. //*****************************************************************************
  20. int main (int argc, char * argv[]) {
  21.  
  22.  parse_arg(argc, argv);
  23.  
  24.  cout << "bits per sample: " << bits_per_sample << endl;
  25.  cout << "device         : " << device << endl;
  26.  cout << "file name      : " << file_name << endl;
  27.  cout << "com port       : " << com_port << endl;
  28.  
  29.  record();
  30.  
  31. };
  32.  
  33.  
  34. //*****************************************************************************
  35. void record() {
  36.  
  37.  MODEM_ENGINE me(com_port);
  38.  
  39.  HFILE com_port;
  40.  ULONG ulAction=0;
  41.  if (DosOpen(file_name,
  42.              &com_port,
  43.              &ulAction,
  44.              0,
  45.              FILE_NORMAL ,
  46.              OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
  47.              OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE,
  48.              0) != NO_ERROR) {
  49.  
  50.     usage();
  51.     exit(0);
  52.  }
  53.  
  54.  
  55.  if (me.mResultCode () == MODEM_ENGINE::noerror) {
  56.  
  57.     cout << endl << "Initializing the modem..." << endl;
  58.  
  59.     me.mInitialize();
  60.     response (me);
  61.  
  62.     me.vmSetDevice(MODEM_ENGINE::voice);
  63.     response (me);
  64.  
  65.     me.vmSetSpeed();
  66.     response (me);
  67.  
  68.     me.vmSetSilenceDetection(FALSE);
  69.     response (me);
  70.  
  71.     switch (bits_per_sample) {
  72.       case 2:
  73.       me.vmSetBits(MODEM_ENGINE::two);
  74.       break;
  75.       case 3:
  76.       me.vmSetBits(MODEM_ENGINE::three);
  77.       break;
  78.       case 4:
  79.       me.vmSetBits(MODEM_ENGINE::four);
  80.       break;
  81.     } /* endswitch */
  82.  
  83.     response (me);
  84.  
  85.     switch (device) {
  86.     case 1:
  87.        me.vmSetLine(MODEM_ENGINE::mic);
  88.        response (me);
  89.  
  90.        break;
  91.  
  92.     case 2: {
  93.        me.vmSetLine(MODEM_ENGINE::phone);
  94.        response (me);
  95.  
  96.        cout << endl << "Pick up the handset." << endl << endl;
  97.  
  98.        char c;
  99.        c= me.mWaitForDLECode();
  100.        while (c != 't') {
  101.           c= me.mWaitForDLECode();
  102.        } /* endwhile */
  103.        }
  104.  
  105.        break;
  106.  
  107.     case 3:
  108.        me.vmSetLine(MODEM_ENGINE::line);
  109.        response (me);
  110.  
  111.        me.mAnswer();
  112.        response (me);
  113.  
  114.        break;
  115.  
  116.     case 4:
  117.        me.vmSetLine(MODEM_ENGINE::lineMonitor);
  118.        response (me);
  119.  
  120.        me.mAnswer();
  121.        response (me);
  122.  
  123.        break;
  124.  
  125.     } /* endswitch */
  126.  
  127.     cout << "Initializing done..." << endl << "Recording file, press enter to stop..." << endl;
  128.  
  129.     me.vmReceive(com_port);
  130.     response (me);
  131.  
  132.     char x[1];
  133.  
  134.     cin.read(x, 1);
  135.  
  136.     MODEM_ENGINE::MODEMRESPONSE mr;
  137.     me.vmStopReceive();
  138.     mr= response (me, 50);
  139.  
  140.     while (mr.code==MODEM_ENGINE::none) {
  141.        me.vmStopTransmit();
  142.        mr= response (me, 50);
  143.     } /* endwhile */
  144.  
  145.     cout << "Recording done..." << endl << "Resetting the modem..." << endl;
  146.  
  147.     me.mInitialize();
  148.     response (me);
  149.  
  150.     cout << "All done..." << endl;
  151.  
  152.  } else {
  153.     cout << "Unable to open the '" << com_port << "' device.\n";
  154.  }
  155. }
  156.  
  157.  
  158. //*****************************************************************************
  159. MODEM_ENGINE::MODEMRESPONSE response (MODEM_ENGINE & me, int timeout) {
  160.  
  161.  MODEM_ENGINE::MODEMRESPONSE mr;
  162.  
  163.     do {
  164.        mr= me.mWaitForModemResponse (timeout);
  165.        if (debug) cout << mr.verbose << endl;
  166.        if (mr.verbose[0]== 0) {
  167.           mr.code= MODEM_ENGINE::none;
  168.        }
  169.     } while ((mr.code != MODEM_ENGINE::ok) &&
  170.              (mr.code != MODEM_ENGINE::error) &&
  171.              (mr.code != MODEM_ENGINE::connect) &&
  172.              (mr.code != MODEM_ENGINE::none) &&
  173.              (mr.code != MODEM_ENGINE::vcon));
  174.  
  175.    return mr;
  176. }
  177.  
  178. //*****************************************************************************
  179. void parse_arg(int argc, char * argv[]) {
  180.  
  181.  for (int i=1; i< argc; i++) {
  182.  
  183.     if (!strncmp(argv[i], "-d:", 3)) {
  184.       device= atoi(argv[i]+3);
  185.       if (device != 1 && device != 2 && device != 3 && device != 4) {
  186.          usage();
  187.          exit(0);
  188.       }
  189.     } else {
  190.      if (!strncmp(argv[i], "-b:", 3)) {
  191.         bits_per_sample= atoi(argv[i]+3);
  192.         if (bits_per_sample != 2 && bits_per_sample != 3 && bits_per_sample != 4) {
  193.            usage();
  194.            exit(0);
  195.         }
  196.         cout << argv[i]+3 << "\n";
  197.      } else {
  198.         if (!strncmp(argv[i], "-f:", 3)) {
  199.  
  200.           file_name= strdup(argv[i]+3);
  201.         } else {
  202.            if (!strncmp(argv[i], "-c:", 3)) {
  203.  
  204.               com_port= strdup(argv[i]+3);
  205.            } else {
  206.  
  207.              if (!strncmp(argv[i], "-de", 3)) {
  208.  
  209.                 debug= TRUE;
  210.              } else {
  211.  
  212.                 usage();
  213.                 exit(0);
  214.              } /* endif */
  215.  
  216.            } /* endif */
  217.         } /* endif */
  218.      } /* endif */
  219.     } /* endif */
  220.  } /* endfor */
  221.  
  222.  if (file_name==NULL || com_port==NULL) {
  223.     usage();
  224.     exit(0);
  225.  } else {
  226.  } /* endif */
  227.  
  228. };
  229.  
  230.  
  231. //*****************************************************************************
  232. void usage(){
  233.  
  234.  cout << "* Required parameters: " << endl << endl
  235.       << "-f:filename, where 'filename' is the name (+ path) of the file to be played." << endl << endl
  236.       << "-c:comport, where 'comport' is the name of the device the voice modem is\n   connected to." << endl << endl
  237.       << "* Optional parameters:" << endl << endl
  238.       << "-b:bits, where 'bits' is either 2, 3 or 4 and stands for the sampling rate of" << endl << " the file to be played, this defaults to 4" << endl << endl
  239.       << "-d:device, where 'device' is either 1, 2, 3 or 4." << endl
  240.       << " 1 stands for modem mic," << endl
  241.       << " 2 stands for phone handset." << endl
  242.       << " 3 stands for telephone line." << endl
  243.       << " 4 stands for telephone line with monitor. This defaults to 1" << endl << endl
  244.       << "-debug turns debugging information on." << endl << endl
  245.       << "* Example:" << endl << endl
  246.       << "record -f:c:\\path\\file.msg -c:com1 -b:3 -d:2" << endl;
  247. };
  248.